package com.bawei.module_mall

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_mall.databinding.ActivityCollectShopBinding
import com.bawei.module_mall.http.intent.ShopIntent
import com.bawei.module_mall.http.state.ShopState
import com.bawei.module_mall.http.viewModel.ShopViewModel
import com.bawei.module_mall.model.Record
import com.bumptech.glide.Glide
import com.bawei.module_mall.databinding.ItemCollectBinding
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_mvi.Router
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@Route(path = Router.Ui.COLLECT_SHOP_ACTIVITY)
class CollectShopActivity : BaseActivity() {

    private val binding by lazy { ActivityCollectShopBinding.inflate(layoutInflater) }
    private val viewModel by viewModels<ShopViewModel>()

    override fun getLayoutRootView() = binding.root

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 返回按钮
        binding.collectBack.setOnClickListener { finish() }

        // RecyclerView初始化
        binding.rvCollect.layoutManager = LinearLayoutManager(this)

        // 监听状态流
        lifecycleScope.launchWhenStarted {
            viewModel.state.collectLatest { state ->
                when (state) {
                    is ShopState.GetCollectSuccess -> {
                        val records = state.data?.records ?: emptyList()
                        binding.rvCollect.adapter = CollectAdapter(records)
                    }
                    is ShopState.Error -> {
                        // 可根据需要弹Toast
                    }
                    else -> {

                    }
                }
            }
        }
        lifecycleScope.launch {
            viewModel.intents.send(ShopIntent.GetCollectShop)
        }
        // 发送意图，请求收藏列表
    }

    // 内部类：收藏Adapter
    class CollectAdapter(private val items: List<Record>) : RecyclerView.Adapter<CollectAdapter.CollectViewHolder>() {
        inner class CollectViewHolder(val binding: ItemCollectBinding) : RecyclerView.ViewHolder(binding.root)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectViewHolder {
            val binding = ItemCollectBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            return CollectViewHolder(binding)
        }

        override fun onBindViewHolder(holder: CollectViewHolder, position: Int) {
            val item = items[position]
            holder.binding.collectText.text = item.title
            holder.binding.collectPrice.text = "￥${item.marketPriceFee / 100.0}"
            holder.binding.collectType.text = item.brandName
            Glide.with(holder.itemView)
                .load(item.mainImgUrl)
                .into(holder.binding.ivCollectImg)
        }

        override fun getItemCount() = items.size
    }
}